package com.alander.nexusgo.im.mapper;

import com.alander.nexusgo.im.model.entity.GroupMessage;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

@Mapper
public interface GroupMessageMapper extends BaseMapper<GroupMessage> {
    
    /**
     * 查询群组的最新消息
     * @param groupId 群组ID
     * @param limit 限制数量
     * @return 消息列表
     */
    default List<GroupMessage> selectRecentMessages(Integer groupId, int limit) {
        LambdaQueryWrapper<GroupMessage> wrapper = Wrappers.lambdaQuery(GroupMessage.class)
            .eq(GroupMessage::getGroupId, groupId)
            .orderByDesc(GroupMessage::getCreateTime)
            .last("LIMIT " + limit);
        return selectList(wrapper);
    }
}